/**
 * 
 */
package app.util;

import java.io.File;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.apache.commons.lang.StringUtils;

import app.constants.CollabVizConstants;
import app.service.xml.XMLService;
import app.xml.model.FileTag;

/**
 * @author poornima
 * 
 */
public class AppUtil {

	public static void createDir(String dirPath) {
		if (dirPath == null || StringUtils.isEmpty(dirPath)) {
			System.out
					.println("ERROR: The incoming dir path string is either null or empty.");
			return;
		}
		File dirName = new File(dirPath);
		boolean isCreated = dirName.mkdir();
		if (isCreated) {
			System.out.println("Directory created : " + dirPath);
		} else {
			System.out.println("Directory exists." + dirPath);
		}
	}

	public static Hashtable<Integer, JLabel> getCalLabelTable(
			List<String> fileNameList, String dir, XMLService xmlService) {
		if (fileNameList == null || fileNameList.isEmpty()) {
			System.out.println("WARN: the incoming file name list if empty.");
			return null;
		}
		AppUtil obj = new AppUtil();
		Hashtable<Integer, JLabel> result = new Hashtable<Integer, JLabel>();
		FileTag fileTag;
		ImageIcon imgIcon = null;
		JLabel label;
		StringBuffer fileTs = new StringBuffer("");
		int count = 0;
		for (String fileName : fileNameList) {
			fileTs = new StringBuffer("");
			try {
				fileTag = (FileTag) xmlService.unmarshall(dir + fileName);
				if (fileTag == null) {
					continue;
				}
				long timeStamp = Long.parseLong((fileName
						.split(CollabVizConstants.DOT_REGEX)[0].split("_"))[1]);
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(timeStamp);
				fileTs.append(cal.get(Calendar.HOUR));
				fileTs.append(":");
				fileTs.append(cal.get(Calendar.MINUTE));
				fileTs.append(" ");
				fileTs.append(cal.get(Calendar.MONTH) + 1);
				fileTs.append("-");
				fileTs.append(cal.get(Calendar.DATE));
				fileTs.append("-");
				fileTs.append(cal.get(Calendar.YEAR));
				// imgIcon = new ImageIcon();
				if (fileTag.getAnnotation() != null
						&& !fileTag.getAnnotation().isEmpty()
						&& fileTag.getNote() != null
						&& !fileTag.getNote().isEmpty()) {
					imgIcon = new ImageIcon(obj.getClass().getResource(
							CollabVizConstants.SL_COMM_NOTES_IMG).getPath());
				} else if (fileTag.getAnnotation() != null
						&& !fileTag.getAnnotation().isEmpty()) {
					imgIcon = new ImageIcon(obj.getClass().getResource(
							CollabVizConstants.SL_COMMENTS_IMG).getPath());
				} else if (fileTag.getNote() != null
						&& !fileTag.getNote().isEmpty()) {
					imgIcon = new ImageIcon(obj.getClass().getResource(
							CollabVizConstants.SL_NOTES_IMG).getPath());
				}
				if (imgIcon != null) {
					// label = new JLabel(fileTs.toString(), imgIcon,
					// Label.LEFT);
					label = new JLabel(fileTs.toString());
					// label.setText(fileTs.toString());
					label.setIcon(imgIcon);
					label.setVerticalTextPosition(SwingConstants.CENTER);
				} else {
					label = new JLabel(fileTs.toString());

				}

				result.put(count++, label);
			} catch (Throwable th) {
				System.out.println("ERROR: error while loading the file: "
						+ fileName);
				th.printStackTrace();
			}
		}
		return result;
	}
}
